package api

import (
	auto "beergo/app/check/auto/controller"
	"beergo/app/check/conf"
	"beergo/app/check/conf/preload"
	model "beergo/app/check/models"
	"beergo/conf/const/code"
	"beergo/lib/event"
	"beergo/lib/mysql"
	"beergo/lib/tool"
	"github.com/beego/beego/v2/server/web"
)

type TaskMyController struct {
	auto.TaskController
}

func init() {
	web.Router(conf.UrlPrefix + "/my/Task/:id", 		&TaskMyController{}, "*:GetAction")
	web.Router(conf.UrlPrefix + "/my/Task/info", 		&TaskMyController{}, "*:InfoAction")
	web.Router(conf.UrlPrefix + "/my/Task/scan", 		&TaskMyController{}, "*:ScanAction")
	web.Router(conf.UrlPrefix + "/my/Task/list", 	    &TaskMyController{}, "*:ListAction")
	web.Router(conf.UrlPrefix + "/my/Task/update", 	&TaskMyController{}, "*:UpdateAction")
	web.Router(conf.UrlPrefix + "/my/Task/create", 	&TaskMyController{}, "*:CreateAction")
	web.Router(conf.UrlPrefix + "/my/Task/copy", 	    &TaskMyController{}, "*:CopyAction")
	web.Router(conf.UrlPrefix + "/my/Task/edit", 	    &TaskMyController{}, "*:EditAction")
	web.Router(conf.UrlPrefix + "/my/Task/delete", 	&TaskMyController{}, "*:DeleteAction")
	web.Router(conf.UrlPrefix + "/my/Task/:id/:field", &TaskMyController{}, "*:SonsAction")
	web.Router(conf.UrlPrefix + "/my/Task/choice", &TaskMyController{}, "*:ChoiceAction")
	web.Router(conf.UrlPrefix + "/my/Task/category", &TaskMyController{}, "*:CategoryAction")
}
func (this *TaskMyController) Prepare() {
	this.Config = preload.TaskConfig
	this.JsonData = make(map[string]interface{})
	this.Repository = model.TaskRepository{}
	this.Model = &model.Task{}
	this.Model.SetCtx(this.Ctx)
	qb := new(mysql.QueryBuilder)
	this.QueryBuilder = qb.CreateFromRequest(this.Ctx).SetEntityName("Task").SetConfig(this.Config)
	user := this.GetUser()
	if user.Id == 0 {
		this.StopError(code.ErrorAuth,"登录过期","")
	}
	if this.QueryBuilder.Conditions == nil {
		this.QueryBuilder.Conditions = make(tool.M)
		this.QueryBuilder.Conditions["user"] = user.Id
	}
	ae := &event.AuthEvent{
		Name:    "TaskAuth",
		Access:  true,
		Message: "",
	}
	event.DispatchToManySync("TaskAuth", ae,this.Ctx)
	if ae.Access == false {
		this.StopError(code.ErrorAccess, ae.Message, "")
	}
}
//func (this *TaskMyController) ListAction() {}
//func (this *TaskMyController) GetAction() {}
//func (this *TaskMyController) InfoAction() {}
//func (this *TaskMyController) ScanAction() {}
func (this *TaskMyController) CreateAction() {
	user := this.GetUser()
	entity := this.Model.New()
	_, err := this.JsonParseModel(entity)
	this.AnaliseErrors(err)
	entity.User = &user
	_, errSave := entity.Save()
	this.AnaliseErrors(errSave)
	this.FillM2M(entity)
	this.ReturnSuccess("OK", entity.Id)
}
//func (this *TaskMyController) UpdateAction() {}
//func (this *TaskMyController) CopyAction() {}
//func (this *TaskMyController) DeleteAction() {}
//func (this *TaskMyController) EditAction() {}
//func (this *TaskMyController) SonsAction() {}
//func (this *TaskMyController) ChoiceAction() {}
//func (this *TaskMyController) CategoryAction() {}
